package com.dodayum.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.dodayum.model.Sheet1Data;
import com.dodayum.model.Sheet2Data;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Author:dodayum
 * Date:2025-10-15
 */
@Service
public class FoundExcelService {

    public void foundExcel(ServletOutputStream servletOutputStream){
        ExcelWriter excelWriter = EasyExcel.write(servletOutputStream).build();

        // Sheet1
        WriteSheet sheet1 = EasyExcel.writerSheet("用户信息").head(Sheet1Data.class).build();
        List<Sheet1Data> data1 = Arrays.asList(
                new Sheet1Data("张三", 25),
                new Sheet1Data("李四", 30)
        );
        excelWriter.write(data1, sheet1);

        // Sheet2
        WriteSheet sheet2 = EasyExcel.writerSheet("产品信息").head(Sheet2Data.class).build();
        List<Sheet2Data> data2 = Arrays.asList(
                new Sheet2Data("手机", new BigDecimal("2999.00")),
                new Sheet2Data("电脑", new BigDecimal("5999.00"))
        );
        excelWriter.write(data2, sheet2);

        // 动态生成更多 Sheet（可选）
        for (int i = 3; i <= 5; i++) {
            WriteSheet sheet = EasyExcel.writerSheet("动态Sheet" + i).head(Sheet1Data.class).build();
            List<Sheet1Data> dynamicData = Collections.singletonList(
                    new Sheet1Data("动态数据" + i, 20 + i)
            );
            excelWriter.write(dynamicData, sheet);
        }

        // 必须调用 finish 才会写入输出流
        excelWriter.finish();
    }

}
